package com.sanpu.kxe.business.trade.presenter;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.provider.ICardProvider;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.trade.bean.FastPayBannerOutput;
import com.sanpu.kxe.business.trade.contract.FastPayComfirmContract;
import com.sanpu.kxe.business.trade.model.TradeModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.utils.ARouterUtils;
import com.sanpu.kxe.rxservice.RequestMap;
import com.sanpu.kxe.utils.PublicProviderUtil;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：快捷native支付
 * Created by 薛勇军 on 2019/3/8.
 */

public class FastPayComfirmPresenter implements FastPayComfirmContract.Presenter {

    private FastPayComfirmContract.View mView;

    private CompositeSubscription mSubscription;

    private String needPic, rlId, payType, paymentAmt;

    public FastPayComfirmPresenter(FastPayComfirmContract.View mView) {
        this.mView = mView;
        this.mSubscription = new CompositeSubscription();
    }

    @Override
    public void initData(String needPic, String rlId, String payType, String paymentAmt) {
        this.needPic = needPic;
        this.rlId = rlId;
        this.payType = payType;
        this.paymentAmt = paymentAmt;
        selectUrmBankPage();
        lvlFee();
    }

    @Override
    public void selectUrmBankPage() {
        final ICardProvider provider = PublicProviderUtil.getProvider(ICardProvider.class);
        if (provider != null) {
            Subscription subscription = provider.selectUrmBankPage("")
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<BankInfoOutput>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(List<BankInfoOutput> bankInfoOutputs) {
                            if (bankInfoOutputs == null || bankInfoOutputs.size() == 0) {
                                provider.openAddBankSetupActivity(mView.getContext(), true);
                                return;
                            }
                            dealBankData(bankInfoOutputs);
                        }
                    });
            mSubscription.add(subscription);
        }
    }

    /**
     * 筛选银行卡列表
     */
    private void dealBankData(List<BankInfoOutput> bankInfoOutputs) {
        List<BankInfoOutput> mOutBankList = new ArrayList<>();
        List<BankInfoOutput> mInBankList = new ArrayList<>();
        for (BankInfoOutput data : bankInfoOutputs) {
            if (CommonConfig.CREDIT_CARD_TYPE.equals(data.getCardType())) {
                mOutBankList.add(data);
            }
            if (TextUtils.equals("Y", needPic)) {
                if (CommonConfig.DEPOSIT_CARD_TYPE.equals(data.getCardType()) && data.isDefStlFlg()) {
                    mInBankList.add(data);
                    mView.showBankInfo(data, false);
                }
            } else {
                if (CommonConfig.DEPOSIT_CARD_TYPE.equals(data.getCardType())) {
                    mInBankList.add(data);
                    if (data.isDefStlFlg())
                        mView.showBankInfo(data, false);
                }
            }
        }
        mView.setBankListData(mOutBankList, mInBankList);
    }

    @Override
    public void quickReportSelect(BankInfoOutput infoOutput) {

    }

    /**
     * 广告
     */
    private void lvlFee() {
        Subscription subscription = TradeModel.getInstance().lvlFee()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<FastPayBannerOutput>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<FastPayBannerOutput> fastPayBannerOutputs) {
                        mView.showBannerView(fastPayBannerOutputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onBannerItemClick(FastPayBannerOutput bannerOutput) {
        if (bannerOutput.getUrl() != null) {
            if (TextUtils.isEmpty(bannerOutput.getUrl().getUrl())) return;
            String url = bannerOutput.getUrl().getUrl();
            if (!bannerOutput.getUrl().hasParameter()) {
                url += "?" + new RequestMap().map2ReqStr();
            }
            IMainProvider provider = PublicProviderUtil.getProvider(IMainProvider.class);
            if (provider != null) {
                provider.openBrowserX5Activity(mView.getContext(), url,
                        bannerOutput.getUrl().hasTitle(), true);
            }
        } else {
            String appNative = bannerOutput.getAppNative().getAndroid();
            if (ARouterUtils.containRouter(appNative)) {
                ARouter.getInstance()
                        .build(appNative)
                        .withTransition(R.anim.in_from_right, R.anim.in_to_left)
                        .navigation();
                ARouter.getInstance().build(appNative);
            }
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
